package sivan.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import sivan.mapper.BlogMapper;

import java.io.InputStream;
import java.util.BitSet;
import java.util.Deque;
import java.util.LinkedList;
import java.util.concurrent.locks.ReentrantLock;

public class MyBatisTest {

    @Test
    public void testMybatis() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream stream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession openSession = sessionFactory.openSession();
        BlogMapper blogMapper = openSession.getMapper(BlogMapper.class);
        System.out.println(blogMapper.findById(1));
        System.out.println(blogMapper.findByName("test"));
        System.out.println(blogMapper.findByNames("test"));
        System.out.println(blogMapper.selectBlog(1, "test"));
    }

    @Test
    public  void testAqs() {
        /**
         * 定义ReentrantLock变量，有带参和不带参的构造方法。默认是非公平锁
         *  public ReentrantLock(boolean fair) {
         *         sync = fair ? new FairSync() : new NonfairSync();
         *  }
         */
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            try{
                System.out.println("线程1开始");
                lock.lock();
                Thread.sleep(1000000000000L);
                System.out.println("线程1---------------");
                lock.unlock();
            } catch (Exception e) {
                lock.unlock();
            }
        });

        Thread t2 = new Thread(() -> {
            try{
                System.out.println("线程2开始");
                lock.lock();
                Thread.sleep(1000000000000L);
                System.out.println("线程2---------------");
                lock.unlock();
            } catch (Exception e) {
                lock.unlock();
            }
        });

        Thread t3 = new Thread(() -> {
            try{
                System.out.println("线程3开始");
                lock.lock();
                Thread.sleep(1000000000000L);
                System.out.println("线程3---------------");
                lock.unlock();
            } catch (Exception e) {
                lock.unlock();
            }
        });

        Thread t4 = new Thread(() -> {
            try{
                System.out.println("线程4开始");
                lock.lock();
                Thread.sleep(1000000000000L);
                System.out.println("线程4---------------");
                lock.unlock();
            } catch (Exception e) {
                lock.unlock();
            }
        });
        lock.lock();
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        System.out.println("---------------");
        lock.unlock();

        while (true) {

        }
    }


}
